package com.wat4m.jerome.jdpcpool.controler;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;

import com.wat4m.jerome.jdpcpool.service.BookService;

@WebServlet(name="BookServlet",urlPatterns="/book/*")
public class BookServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4193283610640068173L;

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println(req.getContextPath());
		System.out.println(req.getPathInfo());
		ObjectMapper om=new ObjectMapper();
		JsonGenerator jsonGenerator = om.getJsonFactory().createJsonGenerator(resp.getOutputStream(), JsonEncoding.UTF8);
		try {
			jsonGenerator.writeObject(new BookService().findAll());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	
}
